#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>

#include "./../../inc/homePage.h"
#include "./../../inc/calculator.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/login.h"
#include "./../../inc/album.h"
#include "./../../inc/music.h"

static int left_ac_temperature            = 24; // 默认温度
static int right_ac_temperature           = 24; // 默认温度
static int ac_top_mode                    = CLOSE_MODE;
static int ac_bottom_mode                 = CLOSE_MODE;
static lv_obj_t * left_temp_label         = NULL;
static lv_obj_t * right_temp_label        = NULL;
static lv_obj_t * middle_top_img          = NULL;
static lv_obj_t * middle_bottom_img       = NULL;
static lv_obj_t * home_music_img          = NULL;
static lv_obj_t * home_music_song_label   = NULL;
static lv_obj_t * home_music_singer_label = NULL;

// lv_obj_t * middle_img               = NULL;
static void show_card1(lv_obj_t * parent);
static void show_card2(lv_obj_t * parent);
static void show_card3(lv_obj_t * parent);
static void left_kt_btn_event_cb(lv_event_t * e);
static void right_kt_btn_event_cb(lv_event_t * e);
static void middle_kt_btn_event_cb(lv_event_t * e);

void show_card(lv_obj_t * parent)
{
    show_card1(parent);
    show_card2(parent);
    show_card3(parent);
}

// 导航
static void show_card1(lv_obj_t * parent)
{
    lv_obj_t * card = lv_obj_create(parent);
    lv_obj_set_size(card, 230, 142);
    lv_obj_set_pos(card, 28, 265);
    lv_obj_set_style_bg_color(card, lv_color_hex(BLACK), 0);
    lv_obj_set_style_bg_opa(card, LV_OPA_50, 0);
    lv_obj_set_style_border_color(card, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(card, LV_OPA_50, 0);
    lv_obj_set_style_radius(card, 10, 0);

    lv_obj_move_foreground(card);                              // 将任务栏移到最前面
    lv_obj_clear_flag(card, LV_OBJ_FLAG_CLICKABLE);            // 不可点击
    lv_obj_clear_flag(card, LV_OBJ_FLAG_SCROLLABLE);           // 不可滚动
    lv_obj_clear_flag(card, LV_OBJ_FLAG_PRESS_LOCK);           // 禁用按压锁定
    lv_obj_clear_flag(card, LV_OBJ_FLAG_GESTURE_BUBBLE);       // 禁用手势冒泡
    lv_obj_set_style_border_opa(card, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(card, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_t * map_img = lv_img_create(card);
    lv_img_set_src(map_img, "S:res/ui/map.jpg");
    lv_obj_center(map_img);

    lv_obj_set_style_clip_corner(card, true, LV_PART_MAIN);
}

// 音乐
static void show_card2(lv_obj_t * parent)
{
    lv_obj_t * card = lv_obj_create(parent);
    lv_obj_set_size(card, 230, 142);
    lv_obj_set_pos(card, 286, 265);
    lv_obj_set_style_bg_color(card, lv_color_hex(BLACK), 0);
    lv_obj_set_style_bg_opa(card, LV_OPA_50, 0);
    lv_obj_set_style_border_color(card, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(card, LV_OPA_50, 0);
    lv_obj_clear_flag(card, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_t * music_img_card = lv_obj_create(card);
    lv_obj_set_size(music_img_card, 80, 80);
    lv_obj_set_style_bg_color(music_img_card, lv_color_hex(WHITE), 0);
    lv_obj_set_style_border_opa(music_img_card, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(music_img_card, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_clear_flag(music_img_card, LV_OBJ_FLAG_SCROLLABLE);           // 不可滚动
    lv_obj_align(music_img_card, LV_ALIGN_CENTER, -65, 0);

    home_music_img = lv_img_create(music_img_card);
    lv_img_set_src(home_music_img, music_small_imgs[current_song]);
    // lv_img_set_zoom(home_music_img, 82);
    lv_obj_center(home_music_img);

    lv_obj_t * container = lv_obj_create(card);
    lv_obj_set_size(container, 110, 80);
    lv_obj_align(container, LV_ALIGN_CENTER, 45, 0);
    lv_obj_set_style_bg_opa(container, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(container, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(container, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_clear_flag(container, LV_OBJ_FLAG_SCROLLABLE);           // 不可滚动
    lv_obj_set_style_radius(container, 8, 0);
    lv_obj_set_style_pad_all(container, 0, 0);

    home_music_song_label = lv_label_create(container);
    lv_label_set_long_mode(home_music_song_label, LV_LABEL_LONG_SCROLL_CIRCULAR); // 循环滚动模式
    lv_label_set_text(home_music_song_label, home_music_names[current_song]);

    lv_obj_set_width(home_music_song_label, 110);
    lv_obj_align(home_music_song_label, LV_ALIGN_TOP_MID, 0, 5);
    lv_obj_set_style_text_color(home_music_song_label, lv_palette_main(LV_PALETTE_BLUE), 0);
    lv_obj_set_style_text_font(home_music_song_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN);

    home_music_singer_label = lv_label_create(container);
    lv_label_set_long_mode(home_music_singer_label, LV_LABEL_LONG_SCROLL_CIRCULAR); // 循环滚动模式
    lv_label_set_text(home_music_singer_label, home_music_singer[current_song]);
    lv_obj_set_width(home_music_singer_label, 110); // 设置宽度以限制文本区域
    lv_obj_align(home_music_singer_label, LV_ALIGN_BOTTOM_MID, 0, -5);
    lv_obj_set_style_text_color(home_music_singer_label, lv_palette_main(LV_PALETTE_BLUE), 0);
    lv_obj_set_style_text_font(home_music_singer_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN);

    lv_obj_add_event_cb(card, open_music, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(music_img_card, open_music, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(home_music_img, open_music, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(container, open_music, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(home_music_song_label, open_music, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(home_music_singer_label, open_music, LV_EVENT_CLICKED, NULL);
}

// 空调
static void show_card3(lv_obj_t * parent)
{
    lv_obj_t * card2 = lv_obj_create(parent);
    lv_obj_set_size(card2, 230, 142);
    lv_obj_set_pos(card2, 544, 265);
    lv_obj_set_style_bg_color(card2, lv_color_hex(BLACK), 0);
    // lv_obj_set_style_bg_opa(card2, LV_OPA_50, 0);
    lv_obj_set_style_border_width(card2, 0, 0);
    lv_obj_clear_flag(card2, LV_OBJ_FLAG_SCROLLABLE);           // 不可滚动
    lv_obj_set_style_border_opa(card2, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(card2, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_set_style_pad_all(card2, 0, 0);

    // -------------------------- 左侧 -----------------------------
    lv_obj_t * left_kt = lv_obj_create(card2);
    lv_obj_set_size(left_kt, 55, 142);
    lv_obj_set_pos(left_kt, 0, 0);
    lv_obj_set_style_bg_color(left_kt, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(left_kt, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(left_kt, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_set_style_pad_all(left_kt, 0, 0);
    lv_obj_clear_flag(left_kt, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_t * left_add_btn = lv_btn_create(left_kt);
    lv_obj_set_style_bg_color(left_add_btn, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_size(left_add_btn, 50, 142);
    lv_obj_set_size(left_add_btn, 48, 48);
    lv_obj_align(left_add_btn, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_border_opa(left_add_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(left_add_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_add_event_cb(left_add_btn, left_kt_btn_event_cb, LV_EVENT_CLICKED, "up");

    lv_obj_t * left_add_img = lv_img_create(left_add_btn);
    lv_img_set_src(left_add_img, "S:res/ui/kt_up.png");
    lv_obj_center(left_add_img);

    // 温度显示标签（添加在中间）
    left_temp_label = lv_label_create(left_kt);
    char temp_str[4];
    sprintf(temp_str, "%d°", left_ac_temperature);
    lv_label_set_text(left_temp_label, temp_str);
    lv_obj_set_style_text_color(left_temp_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色字体

    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(left_temp_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
    lv_obj_align(left_temp_label, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * left_reduce_btn = lv_btn_create(left_kt);
    lv_obj_set_size(left_reduce_btn, 48, 48);
    lv_obj_align(left_reduce_btn, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_bg_color(left_reduce_btn, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_border_opa(left_reduce_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(left_reduce_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_add_event_cb(left_reduce_btn, left_kt_btn_event_cb, LV_EVENT_CLICKED, "down");

    lv_obj_t * left_reduce_img = lv_img_create(left_reduce_btn);

    lv_img_set_src(left_reduce_img, "S:res/ui/kt_down.png");
    lv_obj_center(left_reduce_img);

    // ------------------------- 中间 ------------------------------

    lv_obj_t * middle_kt = lv_obj_create(card2);
    lv_obj_set_size(middle_kt, 120, 142);
    lv_obj_set_pos(middle_kt, 55, 0);
    lv_obj_set_style_bg_color(middle_kt, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(middle_kt, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(middle_kt, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_set_style_pad_all(middle_kt, 0, 0);

    // lv_obj_t * middle_img = lv_img_create(middle_kt);
    // lv_img_set_src(middle_img, "S:res/ui/cold.png");
    // lv_obj_align(middle_img, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * middle_top_btn = lv_btn_create(middle_kt);
    lv_obj_set_size(middle_top_btn, 48, 48);
    lv_obj_align(middle_top_btn, LV_ALIGN_TOP_MID, 0, 10);
    lv_obj_set_style_bg_color(middle_top_btn, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(middle_top_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(middle_top_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_add_event_cb(middle_top_btn, middle_kt_btn_event_cb, LV_EVENT_CLICKED, "top");

    middle_top_img = lv_img_create(middle_top_btn);
    if(ac_top_mode == CLOSE_MODE) {
        lv_img_set_src(middle_top_img, "S:res/ui/op_cw.png");
    } else {
        lv_img_set_src(middle_top_img, "S:res/ui/cl_cw.png");
    }
    lv_obj_center(middle_top_img);

    // lv_obj_add_flag(middle_top_img, LV_OBJ_FLAG_CLICKABLE);

    lv_obj_t * middle_bottom_btn = lv_btn_create(middle_kt);
    lv_obj_set_size(middle_bottom_btn, 48, 48);
    lv_obj_align(middle_bottom_btn, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_style_bg_color(middle_bottom_btn, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(middle_bottom_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(middle_bottom_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_add_event_cb(middle_bottom_btn, middle_kt_btn_event_cb, LV_EVENT_CLICKED, "bottom");

    middle_bottom_img = lv_img_create(middle_bottom_btn);
    if(ac_bottom_mode == CLOSE_MODE) {
        lv_img_set_src(middle_bottom_img, "S:res/ui/op_wxh.png");
    } else {
        lv_img_set_src(middle_bottom_img, "S:res/ui/cl_wxh.png");
    }
    lv_obj_center(middle_bottom_img);

    // ------------------------- 右侧 ------------------------------
    lv_obj_t * right_kt = lv_obj_create(card2);
    lv_obj_set_size(right_kt, 55, 142);
    lv_obj_set_pos(right_kt, 175, 0);
    lv_obj_set_style_bg_color(right_kt, lv_color_hex(BLACK), 0);
    lv_obj_set_style_border_opa(right_kt, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(right_kt, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_set_style_pad_all(right_kt, 0, 0);

    // 右侧 加
    lv_obj_t * right_add_btn = lv_btn_create(right_kt);
    lv_obj_set_style_bg_color(right_add_btn, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_size(right_add_btn, 50, 142);
    lv_obj_set_size(right_add_btn, 48, 48);
    lv_obj_align(right_add_btn, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_border_opa(right_add_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(right_add_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_add_event_cb(right_add_btn, right_kt_btn_event_cb, LV_EVENT_CLICKED, "up");

    lv_obj_t * right_add_img = lv_img_create(right_add_btn);
    lv_img_set_src(right_add_img, "S:res/ui/kt_up.png");
    lv_obj_center(right_add_img);

    // 温度显示标签（添加在中间）
    right_temp_label = lv_label_create(right_kt);
    char temp_str1[4];
    sprintf(temp_str1, "%d°", right_ac_temperature);
    lv_label_set_text(right_temp_label, temp_str1);
    lv_obj_set_style_text_color(right_temp_label, lv_color_hex(0xFFFFFF), LV_PART_MAIN); // 白色字体

    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(right_temp_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
    lv_obj_align(right_temp_label, LV_ALIGN_CENTER, 0, 0);

    // 右侧 减
    lv_obj_t * right_reduce_btn = lv_btn_create(right_kt);
    lv_obj_set_size(right_reduce_btn, 48, 48);
    lv_obj_align(right_reduce_btn, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_bg_color(right_reduce_btn, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_border_opa(right_reduce_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(right_reduce_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    lv_obj_add_event_cb(right_reduce_btn, right_kt_btn_event_cb, LV_EVENT_CLICKED, "down");

    lv_obj_t * right_reduce_img = lv_img_create(right_reduce_btn);

    lv_img_set_src(right_reduce_img, "S:res/ui/kt_down.png");
    lv_obj_center(right_reduce_img);

    lv_img_cache_invalidate_src(NULL);
}

static void left_kt_btn_event_cb(lv_event_t * e)
{
    const char * btn_type = lv_event_get_user_data(e);

    if(left_temp_label == NULL) return;

    if(strcmp(btn_type, "up") == 0) {
        if(left_ac_temperature < 30) {
            left_ac_temperature++;
        }
    } else if(strcmp(btn_type, "down") == 0) {
        if(left_ac_temperature > 16) {
            left_ac_temperature--;
        }
    }

    char temp_str[6];
    sprintf(temp_str, "%d°", left_ac_temperature);
    lv_label_set_text(left_temp_label, temp_str);
}

static void right_kt_btn_event_cb(lv_event_t * e)
{
    const char * btn_type = lv_event_get_user_data(e);

    if(right_temp_label == NULL) return;

    if(strcmp(btn_type, "up") == 0) {
        if(right_ac_temperature < 30) {
            right_ac_temperature++;
        }
    } else if(strcmp(btn_type, "down") == 0) {
        if(right_ac_temperature > 16) {
            right_ac_temperature--;
        }
    }

    char temp_str[6];
    sprintf(temp_str, "%d°", right_ac_temperature);
    lv_label_set_text(right_temp_label, temp_str);
}

static void middle_kt_btn_event_cb(lv_event_t * e)
{
    const char * btn_type = lv_event_get_user_data(e);
    if(strcmp(btn_type, "top") == 0) {
        if(ac_top_mode == CLOSE_MODE) {
            lv_img_set_src(middle_top_img, "S:res/ui/cl_cw.png");
            ac_top_mode = OPEN_MODE;

        } else {
            lv_img_set_src(middle_top_img, "S:res/ui/op_cw.png");
            ac_top_mode = CLOSE_MODE;
        }
    } else if(strcmp(btn_type, "bottom") == 0) {
        if(ac_bottom_mode == CLOSE_MODE) {
            lv_img_set_src(middle_bottom_img, "S:res/ui/cl_wxh.png");
            ac_bottom_mode = OPEN_MODE;
        } else {
            lv_img_set_src(middle_bottom_img, "S:res/ui/op_wxh.png");
            ac_bottom_mode = CLOSE_MODE;
        }
    }
    // printf("ac_top_mode = %d , ac_bootom = %d\n", ac_top_mode, ac_bottom_mode);
}

void change_home_music_card()
{
    lv_img_set_src(home_music_img, music_small_imgs[current_song]);
    lv_label_set_text(home_music_song_label, home_music_names[current_song]);
    lv_label_set_text(home_music_singer_label, home_music_singer[current_song]);
}